<?php

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\ApiController;
use App\LogicService\Contracts\MedicalRecordLogicContract;
use App\LogicService\Contracts\OphthalmologyFundusChangeLogicContract;
use App\LogicService\Contracts\OphthalmologyMacularEdemaLogicContract;
use App\LogicService\Contracts\OphthalmologyOrdinaryLogicContract;
use App\LogicService\Contracts\OphthalmologyRetinopathyLogicContract;
use Illuminate\Http\Request;

class OphthalmologyController extends ApiController
{
    private $request;

    private $ophthalmologyFundusChange;

    private $ophthalmologyOrdinary;

    private $ophthalmologyRetinopathy;

    private $ophthalmologyMacularEdema;

    private $medicalRecord;

    public function __construct(Request $request, OphthalmologyFundusChangeLogicContract $ophthalmologyFundusChange,
                                OphthalmologyOrdinaryLogicContract $ophthalmologyOrdinary, OphthalmologyRetinopathyLogicContract $ophthalmologyRetinopathy,
                                OphthalmologyMacularEdemaLogicContract $ophthalmologyMacularEdema, MedicalRecordLogicContract $medicalRecord)
    {
        parent::__construct();
        $this->request = $request;
        $this->ophthalmologyFundusChange = $ophthalmologyFundusChange;
        $this->ophthalmologyOrdinary = $ophthalmologyOrdinary;
        $this->ophthalmologyRetinopathy = $ophthalmologyRetinopathy;
        $this->ophthalmologyMacularEdema = $ophthalmologyMacularEdema;
        $this->medicalRecord = $medicalRecord;

    }

    public function store()
    {
        $this->validate($this->request, [
            'record_id' => 'required|integer',
            'ordinary_data' => 'array',
            'fundus_change_data' => 'array',
            'retinopathy_data' => 'array',
            'macular_edema_data' => 'array',
        ]);


        $ordinary_data = $this->request->input('ordinary_data');
        $fundus_change_data = $this->request->input('fundus_change_data');
        $retinopathy_data = $this->request->input('retinopathy_data');
        $macular_edema_data = $this->request->input('macular_edema_data');
        $record_id = $this->request->input('record_id');

        $result = array();

        $ordinary_data_result = array();
        if (count($ordinary_data)) {
            foreach ($ordinary_data as $key => $value) {
                $ordinary = $this->ophthalmologyOrdinary->update($record_id, $value);
                $ordinary_data_result[] = $ordinary;
            }
        }


        $fundus_change_data_result = array();
        if (count($fundus_change_data)) {
            foreach ($fundus_change_data as $key => $value) {
                $fundus_change = $this->ophthalmologyFundusChange->update($record_id, $value);
                $fundus_change_data_result[] = $fundus_change;
            }
        }
        $retinopathy_data_result = array();
        if (count($retinopathy_data)) {

            $retinopathy_data_result = $this->ophthalmologyRetinopathy->update($record_id, $retinopathy_data);
        }
        $macular_edema_data_result = array();
        if (count($macular_edema_data)) {

            $macular_edema_data_result = $this->ophthalmologyMacularEdema->update($record_id, $macular_edema_data);
        }
        $result['ordinary_data'] = $ordinary_data_result;
        $result['fundus_change_data'] = $fundus_change_data_result;
        $result['retinopathy_data'] = $retinopathy_data_result;
        $result['macular_edema_data'] = $macular_edema_data_result;


        return $this->response($result);
    }

    public function getList()
    {

        $this->validate($this->request, [
            'record_id' => 'required|integer',
        ]);

        $record_id = $this->request->input('record_id');
        $result = array();

        $ordinary_data_result = $this->ophthalmologyOrdinary->getList($record_id);
        $fundus_change_data_result = $this->ophthalmologyFundusChange->getList($record_id);
        $retinopathy_data_result = $this->ophthalmologyRetinopathy->show($record_id);
        $macular_edema_data_result = $this->ophthalmologyMacularEdema->show($record_id);

        $result['ordinary_data'] = $ordinary_data_result;
        $result['fundus_change_data'] = $fundus_change_data_result;
        $result['retinopathy_data'] = $retinopathy_data_result;
        $result['macular_edema_data'] = $macular_edema_data_result;

        return $this->response($result);
    }


    public function getLastList()
    {
        $this->validate($this->request, [
            'record_id' => 'integer',
            'type' => 'required|integer',
            'patient_id' => 'integer'
        ]);

        $record_id = $this->request->input('record_id');
        $type = $this->request->input('type');
        $patient_id = $this->request->input('patient_id');
        $result = array();

        $last_record = $this->medicalRecord->getLastRecord($record_id, $type, $patient_id);

        $ordinary_data_result = $this->ophthalmologyOrdinary->getList($last_record->id);
        $fundus_change_data_result = $this->ophthalmologyFundusChange->getList($last_record->id);
        $retinopathy_data_result = $this->ophthalmologyRetinopathy->show($last_record->id);
        $macular_edema_data_result = $this->ophthalmologyMacularEdema->show($last_record->id);

        $result['ordinary_data'] = $ordinary_data_result;
        $result['fundus_change_data'] = $fundus_change_data_result;
        $result['retinopathy_data'] = $retinopathy_data_result;
        $result['macular_edema_data'] = $macular_edema_data_result;

        return $this->response($result);
    }


    public function isSave()
    {
        $this->validate($this->request, [
            'record_id' => 'required|integer'
        ]);

        $record_id = $this->request->input('record_id');

        $ordinary_data_result = $this->ophthalmologyOrdinary->getList($record_id);
        $fundus_change_data_result = $this->ophthalmologyFundusChange->getList($record_id);
        $retinopathy_data_result = $this->ophthalmologyRetinopathy->show($record_id);
        $macular_edema_data_result = $this->ophthalmologyMacularEdema->show($record_id);

        $is_save = 0;

        if ($ordinary_data_result || $fundus_change_data_result || $retinopathy_data_result || $macular_edema_data_result) {
            $is_save = 1;
        }

        $result['is_save'] = $is_save;

        return $this->response($result);
    }


}
